package com.baifengz.search;



import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.Comparator;
import java.util.Vector;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class binary_search01 {


    public static int binary_searchBasic(Integer a[],Integer  target) {
        int l=0,r=a.length-1;
        while(l<=r){
            int mid=(l+r)/2;
            if(a[mid]< target){
                l=mid+1;
            } else if (a[mid]>target) {
                r=mid-1;
            }else {
                return mid;
            }
        }
        return -1;
    }

    @Test
    @DisplayName("二分查找找到")
    public void test1(){
        Integer a[]={1,2,3,5,4,6,7,8,9,10};
        Arrays.sort(a);
//        Arrays.sort(a,Comparator.reverseOrder());
        assertEquals(0,binary_searchBasic(a,1));

    }

    @Test
    @DisplayName("二分查未找到")
    public void test2(){
        Integer a[]={1,2,3,5,4,6,7,8,9,10};
        Arrays.sort(a);
//        Arrays.sort(a,Comparator.reverseOrder());
      assertEquals(-1,binary_searchBasic(a,0));

    }


}
